package servlet;

import gameserver.SocketServer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modle.Member;

import org.hibernate.Session;

import util.SessionFactory;
/**
 * 主要实现跳转到游戏房间界面
 * @author GaoTong
 *	
 */
public class JoinRoomServlet extends HttpServlet {
	
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//这里的roomId是数据量的 id-1
		int roomId = Integer.parseInt(request.getParameter("roomId"))  - 1;
		SocketServer server = StartSocketServlet.socketServerList.get(roomId);
		int port = server.getPort();
		
		HttpSession session = request.getSession();
//		if()
		
		String playername = (String)session.getAttribute("username");
    	if(playername == null){
    		playername = (String) session.getAttribute("guestName");
    	}
    	if(playername == null){ //这个应该是执行不到的，防止playername为null.  说明用户是直接输入的地址进入
//    		playername = request.getRemoteAddr();
    		response.sendRedirect("http://"+request.getServerName()+"/Joomla/index.php/game");
    		response.flushBuffer();
    		return;
    	}
		
    	//设置该房间  桌子的类型
    	request.setAttribute("type", server.type);
 
    	request.setAttribute("playername", playername);
    	request.setAttribute("port", port+"");
    	request.setAttribute( "roomId", roomId+"");
    	
    	
    	
		ArrayList<Integer> list = SocketServer.userRoomRecord.get(playername);
		
		
		System.out.println("可以用list: 现在该用户同时进入两个房间" + list);
		
		if(list == null){
			list = new ArrayList<Integer>();
			list.add(port);
			SocketServer.userRoomRecord.put(playername, list);
			
			//放在request中的一些信息，  以便在游戏中使用
	    	
		}else{
			
			//说明用户已经在此房间登录。
			if(list.contains(port)){
				//然后再判断下cookie， 看用户是不是刷新的当前页面
//				Cookie[] cookies  = request.getCookies();
//				for(Cookie c:cookies){
//					if( !c.getValue().equals(roomId+"")){ //如过没有该Cookie说明是，重复进入
//						System.out.println("cookies:" + c.getValue());
//						request.getRequestDispatcher("error/warning.jsp").forward(request, response);
//						return;
//					}
//				}
				
				
			}else{
				
				list.add(port);
			}
			
			
		}
		

		
		
		request.getRequestDispatcher("game.jsp").forward(request, response);
		
	
		out.flush();
		out.close();
	}

}
